import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.Hashtable;

public class StudentManager extends JFrame implements ActionListener {
    // Commenting out for now assuming StudentSituation is not provided
    // StudentSituation 基本信息录入 = null;
    ModifySituation 基本信息修改 = null;
    Message 基本信息查询 = null;
    Delete 基本信息删除 = null;
    JMenuBar bar;
    JMenu fileMenu;
    JMenuItem 录入, 修改, 查询, 删除;
    Container con = null;
    Hashtable<String, Student> 基本信息 = null; // Updated Hashtable declaration
    File file = null;
    CardLayout card = null;
    JLabel label = null;
    JPanel pCenter;

    public StudentManager() {
        录入 = new JMenuItem("录入学生基本信息");
        修改 = new JMenuItem("修改学生基本信息");
        查询 = new JMenuItem("查询学生基本信息");
        删除 = new JMenuItem("删除学生基本信息");
        bar = new JMenuBar();
        fileMenu = new JMenu("菜单选项");
        fileMenu.add(录入);
        fileMenu.add(修改);
        fileMenu.add(查询);
        fileMenu.add(删除);
        bar.add(fileMenu);
        setJMenuBar(bar);
        label = new JLabel("欢迎使用学籍管理系统！", JLabel.CENTER);
        label.setFont(new Font("TimesRoman", Font.BOLD, 40));
        label.setForeground(Color.red);
        基本信息 = new Hashtable<String, Student>(); // Initialize Hashtable with String, Student pair
        录入.addActionListener(this);
        修改.addActionListener(this);
        查询.addActionListener(this);
        删除.addActionListener(this);
        card = new CardLayout();
        con = getContentPane();
        pCenter = new JPanel();
        pCenter.setLayout(card);
        file = new File("基本信息.txt");
        if (!file.exists()) {
            try {
                file.createNewFile(); // Create the file if it doesn't exist
                saveHashtable(); // Save empty Hashtable to the file
            } catch (IOException e) {
                e.printStackTrace(); // Handle IOException appropriately
            }
        } else {
            loadHashtable(); // Load Hashtable from the file if it exists
        }

        // Assuming StudentSituation, ModifySituation, Message, and Delete are classes in your project
        // 基本信息录入 = new StudentSituation(file);
        基本信息修改 = new ModifySituation(file);
        基本信息查询 = new Message(this, file);
        基本信息删除 = new Delete(file);

        pCenter.add("欢迎语界面", label);
        // pCenter.add("录入界面", 基本信息录入);
        pCenter.add("修改界面", 基本信息修改);
        pCenter.add("删除界面", 基本信息删除);
        con.add(pCenter, BorderLayout.CENTER);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                saveHashtable(); // Save Hashtable to the file when closing the window
                System.exit(0);
            }
        });

        setVisible(true);
        setSize(820, 720); // Use setSize instead of setBounds
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Ensure the application exits on close
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == 录入) {
            // card.show(pCenter, "录入界面");
        } else if (e.getSource() == 修改) {
            card.show(pCenter, "修改界面");
        } else if (e.getSource() == 查询) {
            基本信息查询.setVisible(true);
        } else if (e.getSource() == 删除) {
            card.show(pCenter, "删除界面");
        }
    }

    private void saveHashtable() {
        try (FileOutputStream out = new FileOutputStream(file);
             ObjectOutputStream objectOut = new ObjectOutputStream(out)) {
            objectOut.writeObject(基本信息);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void loadHashtable() {
        try (FileInputStream in = new FileInputStream(file);
             ObjectInputStream objectIn = new ObjectInputStream(in)) {
            基本信息 = (Hashtable<String, Student>) objectIn.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(() -> new StudentManager());
    }
}